package cn.kgc.chenxiao.controller;

import cn.kgc.chenxiao.pojo.Detail;
import cn.kgc.chenxiao.service.DetailService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @author 陈啸
 * @create 2020-01-06 9:35
 */
@Controller
@RequestMapping(value = "detail")
public class DetailController {
    @Resource
    DetailService detailService;

    @RequestMapping(value = "toLogin",method = RequestMethod.GET)
    public String getSelect(@RequestParam(value = "categoryId",required = false,defaultValue = "0") int categoryId, @RequestParam(value = "title",required = false) String title, HttpSession session){
        List<Detail> list=detailService.getSelect(categoryId,title);
        session.setAttribute("list",list);
        session.setAttribute("categoryId",categoryId);
        session.setAttribute("title",title);
        return "index";
    }

    @RequestMapping(value = "ToUpdate", method = RequestMethod.GET)
    public String index(int id,HttpSession session) {
        Detail detail=detailService.getUpdateById(id);
        session.setAttribute("detail",detail);
        return "update";
    }
    @RequestMapping(value = "Update", method = RequestMethod.POST)
    public String index(Detail detail) {
        detailService.getUpdate(detail);
        return "redirect:/detail/toLogin";
    }

    @RequestMapping(value = "/title", method = RequestMethod.GET)
    @ResponseBody
    public String index(String title) {
        HashMap<String ,String > resultMap=new HashMap<>();
        Detail detail=detailService.getByTitle(title);
        if(detail==null){
            resultMap.put("title","hhh");
        }else{
            resultMap.put("title","aaa");
        }
        return JSON.toJSONString(resultMap);
    }

}
